﻿#region File Header
/***********************************************************************
 * Copyright © 2012 Beals Software
 * All Rights Reserved
************************************************************************
Author: Donald Beals
Date: Month Day, Year
Description: TODO: Write a description of this file here.
****************************** Change Log ******************************
MM.DD.YY - Created initial file. (dbeals)
***********************************************************************/
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
#endregion

namespace PolygonBasedPathfinding
{
	/// <summary>
	/// 
	/// </summary>
	public sealed class Path
	{
		#region Variables
		private List<Vector2> waypoints = null;
		#endregion

		#region Properties
		public Vector2[] Waypoints
		{
			get
			{
				return waypoints.ToArray();
			}
			set
			{
				waypoints = new List<Vector2>(value == null ? new Vector2[0] : value);
			}
		}

		public Vector2? NextWaypoint
		{
			get
			{
				return waypoints.Count == 0 ? (Vector2?)null : waypoints.First();
			}
		}

		public Vector2? LastWaypoint
		{
			get
			{
				return waypoints.Count == 0 ? (Vector2?)null : waypoints.Last();
			}
		}

		public int Length
		{
			get
			{
				return waypoints.Count;
			}
		}
		#endregion

		#region Constructors
		public Path()
		{
			waypoints = new List<Vector2>();
		}
		#endregion

		#region Methods
		public Vector2 GetDistanceToNextWaypoint(Vector2 position)
		{
			return NextWaypoint == null ? Vector2.Zero : NextWaypoint.Value - position;
		}

		public Vector2 GetDirectionVectorToNextWaypoint(Vector2 position)
		{
			return NextWaypoint == null ? Vector2.Zero : Vector2.Normalize(NextWaypoint.Value - position);
		}

		public void AddWaypoint(float x, float y)
		{
			AddWaypoint(new Vector2(x, y));
		}

		public void AddWaypoint(Vector2 waypoint)
		{
			waypoints.Add(waypoint);
		}

		public void AddWaypoints(IEnumerable<Vector2> newWaypoints)
		{
			waypoints.AddRange(newWaypoints);
		}

		public Vector2 PopWaypoint()
		{
			if(waypoints == null || waypoints.Count == 0)
				return Vector2.Zero;
			var output = waypoints.First();
			waypoints.RemoveAt(0);
			return output;
		}

		public void Clear()
		{
			waypoints.Clear();
		}
		#endregion
	}
}
